package com.wanxi.service.impl;

import com.alibaba.fastjson.JSON;
import com.wanxi.dto.OrderDTO;
import com.wanxi.dto.ProductDTO;
import com.wanxi.feign.*;
import com.wanxi.query.OrderQuery;
import com.wanxi.model.PatientQuery;
import com.wanxi.model.ProductInstanceQuery;
import com.wanxi.model.ProductQuery;
import com.wanxi.result.CommonResult;
import com.wanxi.service.FrontGuardianService;
import com.wanxi.util.OrderNumberUtil;
import com.wanxi.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.List;
@Service
public class FrontGuardianServiceImpl implements FrontGuardianService {

    @Autowired
    private UserFeignClient userFeignClient;

    @Autowired
    private PatientClient patientClient;

    @Autowired
    private OrderNumberUtil orderNumberUtil;

    @Autowired
    private OrderClient orderClient;

    @Autowired
    private ProductFeignClient productFeignClient;

    @Autowired
    private DoctorFeignClient doctorFeignClient;


    @Override
    public CommonResult addUser(UserFrontDTO user) {
        return userFeignClient.addUser(user);
    }

    @Override
    public CommonResult updateGuardianState(Long id, Integer state) {
        return userFeignClient.updateGuardianState(id, state);
    }

    @Override
    public CommonResult perfectInformation(UserInfoDTO user) {
        return userFeignClient.perfectInformation(user);
    }

    @Override
    public CommonResult getGuardianByCondition(UserQuery userQuery) {
        return userFeignClient.getGuardianByCondition(userQuery);
    }

    @Override
    public CommonResult findPatientByGuardianId(Long guardianId) {
        //设置查询条件guardianId的值
        PatientQuery patientQuery = new PatientQuery();
        patientQuery.setGuardianId(guardianId);
        return patientClient.findPatient(patientQuery);
    }

    @Override
    public CommonResult<?> createOrder(OrderDTO orderVO) {
        //生成订单号
        String orderNumber = this.orderNumberUtil.getOrderNumberUtil();
        orderVO.setOrderNumber(orderNumber);
        OrderDTO orderDTO = new OrderDTO();
        BeanUtils.copyProperties(orderVO,orderDTO);
        CommonResult<?> addOrder = orderClient.addOrder(orderDTO);
        if (addOrder.getCode() == 200){
            //获取购买的监护人信息
            UserQuery userQuery = new UserQuery();
            userQuery.setGuardianId(orderDTO.getUserId());
            CommonResult userFrontCommonResult = userFeignClient.getUserFront(userQuery);
            UserDTO userDTO;
            if (userFrontCommonResult.getCode() == 200){
                List<LinkedHashMap> users = (List<LinkedHashMap>) userFrontCommonResult.getData();
                LinkedHashMap userVOHashMap = users.get(0);
                userDTO = JSON.parseObject(JSON.toJSONString(userVOHashMap), UserDTO.class);
            }else {
                return CommonResult.failed();
            }
            //获取服务包信息
            ProductQuery productQuery = new ProductQuery();
            productQuery.setProductId(orderDTO.getProductId());
            CommonResult productCommonResult = productFeignClient.findProduct(productQuery);
            ProductDTO productDTO;
            if (productCommonResult.getCode() == 200){
                List<LinkedHashMap> products = (List<LinkedHashMap>) productCommonResult.getData();
                LinkedHashMap productVoHashMap = products.get(0);
                productDTO = JSON.parseObject(JSON.toJSONString(productVoHashMap), ProductDTO.class);
            }else {
                return CommonResult.failed();
            }
            //封装结果
            OrderResult orderResult = new OrderResult();
            orderResult.setOrderNumber(orderNumber);
            orderResult.setOrderAmount(orderDTO.getOrderAmount());
            orderResult.setOrderState(orderDTO.getOrderState());
            orderResult.setUserDTO(userDTO);
            orderResult.setProductDTO(productDTO);
            return CommonResult.success(orderResult);
        }else {
            return CommonResult.failed();
        }
    }

    @Override
    public CommonResult<?> findOrderByGuardianId(Long guardianId, Integer orderState) {
        OrderQuery orderQuery = new OrderQuery();
        orderQuery.setUserId(guardianId);
        orderQuery.setOrderState(orderState);
        return orderClient.findOrder(orderQuery);
    }

    @Override
    public CommonResult addAuthentication(DoctorApplyDTO doctorApplyDTO) {
        return doctorFeignClient.addApply(doctorApplyDTO);
    }

    @Override
    public CommonResult findAccountStatus(Long guardianId, Integer businessStatus) {
        DoctorApplyQuery doctorApplyQuery  = new DoctorApplyQuery();
        doctorApplyQuery.setUserId(guardianId);
        doctorApplyQuery.setState(businessStatus);
        return doctorFeignClient.findAccountApply(doctorApplyQuery);
    }

    @Override
    public CommonResult addPatientApply(PatientApplyDTO patientApplyDTO) {
        return patientClient.addPatientApply(patientApplyDTO);
    }

    @Override
    public CommonResult findProductEntityByGuardianId(Long guardianId, Integer serviceStatus) {
        ProductInstanceQuery productInstanceQuery = new ProductInstanceQuery();
        productInstanceQuery.setGuardianId(guardianId);
        productInstanceQuery.setServiceStatus(serviceStatus);
        return productFeignClient.findProductInstanceBack(productInstanceQuery);
    }

    @Override
    public CommonResult getUserFront(UserQuery userQuery) {
        return userFeignClient.getUserFront(userQuery);
    }


    @Override
    public CommonResult SwitchDoctor(Long id) {
        return userFeignClient.SwitchDoctor(id);
    }


//    @Autowired
//    private UserFeignClient userFeignClient;
//
//    @Autowired
//    private OrderClient orderFeignClient;
//
//    @Autowired
//    private DoctorFeignClient doctorFeignClient;
//
//    @Autowired
//    private PatientClient patientClient;
//
//    @Autowired
//    private BackDoctorFeignClient backDoctorFeignClient;
//
//    @Autowired
//    private ProductEntityFeignClient productEntityFeignClient;
//
//    @Override
//    public CommonResult findGuardian(Long id) {
//        UserQuery  userQuery  = new UserQuery();
//        userQuery.setGuardianId(id);
//        CommonResult commonResult = userFeignClient.getUserFront(userQuery);
//        if (commonResult.getCode() == 200){
//            return commonResult;
//        }else {
//            return CommonResult.failed();
//        }
//    }
//
//    @Override
//    public CommonResult perfectInformation(Long id, UserFrontDTO userFrontVO) {
//        CommonResult commonResult = userFeignClient.perfectInformation(userFrontVO);
//        if (commonResult.getCode() == 200){
//            return commonResult;
//        }else {
//            return CommonResult.failed();
//        }
//    }
//
//    @Override
//    public CommonResult findPatientByGuardianId(Long guardianId) {
//        PatientQuery patientQuery = new PatientQuery();
//        patientQuery.setGuardianId(guardianId);
//        CommonResult commonResult = patientClient.findPatient(patientQuery);
//        if (commonResult.getCode() == 200){
//            return commonResult;
//        }else {
//            return CommonResult.failed();
//        }
//    }
//
//    @Override
//    public CommonResult<?> createOrder(OrderRequestVo orderRequestVo) {
//        CommonResult<?> commonResult = orderFeignClient.create(orderRequestVo);
//        if (commonResult.getCode() == 200){
//            return commonResult;
//        }else {
//            return CommonResult.failed();
//        }
//    }
//
//    @Override
//    public CommonResult<?> pay(PayRequestVo payRequestVo) {
//        CommonResult<?> commonResult = orderFeignClient.pay(payRequestVo);
//        if (commonResult.getCode() == 200){
//            return commonResult;
//        }else {
//            return CommonResult.failed();
//        }
//    }
//
//    @Override
//    public CommonResult<?> findOrderByGuardianId(Long guardianId, Integer orderState) {
//        OrderClassiFyRequestVo orderClassiFyRequestVo = new OrderClassiFyRequestVo();
//        orderClassiFyRequestVo.setUserId(guardianId);
//        orderClassiFyRequestVo.setOrderState(orderState);
//        CommonResult<?> commonResult = orderFeignClient.orderState(orderClassiFyRequestVo);
//        if (commonResult.getCode() == 200){
//            return commonResult;
//        }else {
//            return CommonResult.failed();
//        }
//    }
//
//    @Override
//    public CommonResult addAuthentication(DoctorAdd doctorAdd) {
//        CommonResult commonResult = doctorFeignClient.addAuthentication(doctorAdd);
//        if (commonResult.getCode() == 200){
//            return commonResult;
//        }else {
//            return CommonResult.failed();
//        }
//    }
//
//    @Override
//    public CommonResult findAccountStatus(Long guardianId, Integer businessStatus) {
//        DoctorAccountStatus doctorAccountStatus = new DoctorAccountStatus();
//        doctorAccountStatus.setId(guardianId);
//        doctorAccountStatus.setState(businessStatus);
//        CommonResult commonResult = backDoctorFeignClient.findAccountStatus(doctorAccountStatus);
//        if (commonResult.getCode() == 200){
//            return commonResult;
//        }else {
//            return CommonResult.failed();
//        }
//    }
//
//    @Override
//    public CommonResult addPatientApply(PatientApplyDTO patientApplyVO) {
//        return null;
//    }
//
//    @Override
//    public CommonResult findProductEntityByGuardianId(Long guardianId, Integer serviceStatus) {
//        ProductEntityQuery productEntityQuery = new ProductEntityQuery();
//        productEntityQuery.setGuardianId(guardianId);
//        productEntityQuery.setServiceStatus(serviceStatus);
//        CommonResult commonResult = productEntityFeignClient.findProductEntity(productEntityQuery);
//        if (commonResult.getCode() == 200){
//            return commonResult;
//        }else {
//            return CommonResult.failed();
//        }
//    }
//
//    @Override
//    public CommonResult switchDoctor(Long id) {
//        CommonResult commonResult = userFeignClient.findSettledStatus(id);
//        if (commonResult.getCode() == 200){
//            return commonResult;
//        }else {
//            return CommonResult.failed();
//        }
//    }
}
